
## source qnxsdp-env.sh first !!!
############################################################
BUILD_PATH = out
PROGRAM_NAME = bac_inet_server.out
SRC_DIR = src/

PREBUILTS_DIR = 
LIBS_GENERAL := 
LIBS_NON_STANDARD := 

LINK_MODULES := -l socket

INCLUDE_PATHS += ./inc

#############################################################
#LOCAL_CC = q++
#LOCAL_ARCH_DEF = -Vgcc_ntoaarch64le
#LOCAL_GENERATE_MAP = -Map,$(RELEASE_PATH)/$(PROGRAM_NAME).map
##### use either q++ or ntoaarch64-gcc-8.3.0
LOCAL_CC = ntoaarch64-gcc-8.3.0
LOCAL_CCP = ntoaarch64-g++-8.3.0
#############################################################


DEPFLAGS = -MT $@ -MMD -MP -MF

LOCAL_CFLAGS +=  $(LOCAL_ARCH_DEF) ${MAKELDFLAGS} -O0 -g
LOCAL_CCFLAGS +=  $(LOCAL_ARCH_DEF)${MAKELDFLAGS} -O0 -g

LINK_OPTIONS += $(LOCAL_ARCH_DEF) $(LINK_MODULES) 

LOCAL_PATH := $(shell pwd)

C_SOURCES += $(wildcard $(SRC_DIR)*.c)
CPP_SOURCES += $(wildcard $(SRC_DIR)*.cpp)

ALL_SOURCES := $(C_SOURCES) $(CPP_SOURCES)

INCLUDES := $(patsubst %,-I%, $(INCLUDE_PATHS))

OBJECTS = $(patsubst $(SRC_DIR)%.cpp,%.o,$(patsubst $(SRC_DIR)%.c,%.o,$(ALL_SOURCES)))

# This shall be within the app's 'build' folder
RELEASE_PATH = $(BUILD_PATH)/release

LIBS_QNX_PREBUILT := # pmem_client ssr slog2 amfs2S audio_log_utilS audio_utilsS 

# All libs list
LIBS := $(LIBS_GENERAL) $(LIBS_NON_STANDARD)

LIBS_INC_PATH := $(patsubst %,-L%, $(dir $(LIBS)))

#LIBS_INC_PATH += \
	-L$(PREBUILTS_DIR) \
	-Wl,--rpath-link,$(PREBUILTS_DIR)

# Convert file lists to parameters for toolchain
LIBS_INC_GENERAL = $(patsubst %.a,-l:%.a,$(notdir $(LIBS_GENERAL)))
LIBS_INC_NON_STANDARD = $(patsubst %.a,-l:%.a,$(notdir $(LIBS_NON_STANDARD)))
LIBS_INC_QNX_PREBUILT = $(patsubst %,-l%,$(LIBS_QNX_PREBUILT))

LIBS_INC = $(LIBS_INC_GENERAL) $(LIBS_INC_NON_STANDARD) $(LIBS_INC_QNX_PREBUILT)
APPLICATION_OUTPUT = $(RELEASE_PATH)/$(PROGRAM_NAME)

############################# Rules
all: $(PROGRAM_NAME)

ALL_BUILD_PATHS ?= \
                  $(BUILD_PATH) \
                  $(BUILD_PATH)/obj 

$(ALL_BUILD_PATHS):
	@mkdir -p $@

$(PROGRAM_NAME): $(APPLICATION_OUTPUT)

$(BUILD_PATH)/obj/%.o:$(SRC_DIR)%.cpp
        @if [ "$(QNX_HOST)" = "" ]; then \
        echo "You will source qnxsdp-env.sh first."; \
        fi
	@echo "Building: $*.o"
	$(LOCAL_CC) $(LOCAL_CFLAGS) $(INCLUDES) $(SRC_DIR)$*.cpp -c -o $(BUILD_PATH)/obj/$*.o

$(BUILD_PATH)/obj/%.o:$(SRC_DIR)%.c
	@echo "Building: $*.o"
	$(LOCAL_CCP) $(LOCAL_CCFLAGS) $(INCLUDES) $(SRC_DIR)$*.c -c -o $(BUILD_PATH)/obj/$*.o

$(APPLICATION_OUTPUT): $(ALL_BUILD_PATHS) $(LIBS) $(addprefix $(BUILD_PATH)/obj/,$(OBJECTS))
	@echo ""
	@echo "Link Executable and generate release"
	@mkdir -p $(RELEASE_PATH)
	$(LOCAL_CCP) $(LINK_OPTIONS) $(LOCAL_GENERATE_MAP) -o $(RELEASE_PATH)/$(PROGRAM_NAME) $(addprefix $(BUILD_PATH)/obj/,$(OBJECTS)) $(LIBS_INC_PATH) $(LIBS_INC) $(LRFLAGS)

info:
	@echo MAKE DEBUGGING LINES:
	@echo $(CPP_SOURCES)
	@echo $(C_SOURCES)
	@echo $(LOCAL_CC) $(OBJECTS)
	@echo $(APPLICATION_OUTPUT)
	@echo $(LIBS)
	@echo $(LIBS_INC_PATH)
	@echo $(INCLUDE_PATHS)

.PHONY:clean

clean:
	rm -rf $(BUILD_PATH)
